package com.gryffonx.Interfaces.NPC;

import com.gryffonx.utils.TimelineTableCellRenderer;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

/**
 * **************************************************************************
 * Application:
 * Author: Erik Stell
 * Date: Feb 17, 2005
 * Time: 8:11:51 AM
 * Process:
 * **************************************************************************
 */
public class NPCListBinding {
    private JPanel pnlMainPnl;
    private JPanel pnlButtons;
    private JButton btnClose;
    private JButton btnDelete;
    private JButton btnEdit;
    private JButton btnAdd;
    private JScrollPane scrlrNpcs;
    private JTable tblNpcs;

    public JPanel getPnlMainPnl() {
        return pnlMainPnl;
    }

    public void setPnlMainPnl(JPanel pnlMainPnl) {
        this.pnlMainPnl = pnlMainPnl;
    }

    public JPanel getPnlButtons() {
        return pnlButtons;
    }

    public void setPnlButtons(JPanel pnlButtons) {
        this.pnlButtons = pnlButtons;
    }

    public JButton getBtnClose() {
        return btnClose;
    }

    public void setBtnClose(JButton btnClose) {
        this.btnClose = btnClose;
    }

    public JButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(JButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public JButton getBtnEdit() {
        return btnEdit;
    }

    public void setBtnEdit(JButton btnEdit) {
        this.btnEdit = btnEdit;
    }

    public JButton getBtnAdd() {
        return btnAdd;
    }

    public void setBtnAdd(JButton btnAdd) {
        this.btnAdd = btnAdd;
    }

    public JScrollPane getScrlrNpcs() {
        return scrlrNpcs;
    }

    public void setScrlrNpcs(JScrollPane scrlrNpcs) {
        this.scrlrNpcs = scrlrNpcs;
    }

    public JTable getTblNpcs() {
        return tblNpcs;
    }

    public void setTblNpcs(JTable tblNpcs) {
        this.tblNpcs = tblNpcs;
    }

    public NPCListBinding() {


        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();

        //populate the table
        Object[] colName = new Object[5];
        colName[0] = "ID";
        colName[1] = "Name";
        colName[2] = "Race";
        colName[3] = "Class";
        colName[4] = "Hit Points";


        DefaultTableModel generatedTableModel = new DefaultTableModel(colName, 0);

        tblNpcs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        tblNpcs.setModel(generatedTableModel);
        tblNpcs.setShowGrid(true);

        TimelineTableCellRenderer cellRenderer = new TimelineTableCellRenderer();

        TableColumnModel colModel1 = tblNpcs.getColumnModel();
        colModel1.getColumn(0).setCellRenderer(cellRenderer);
        colModel1.getColumn(1).setCellRenderer(cellRenderer);
        colModel1.getColumn(2).setCellRenderer(cellRenderer);
        colModel1.getColumn(3).setCellRenderer(cellRenderer);
        colModel1.getColumn(4).setCellRenderer(cellRenderer);

        tblNpcs.setColumnSelectionAllowed(false);
        tblNpcs.setRowSelectionAllowed(false);
        tblNpcs.setCellSelectionEnabled(false);

        //get the xml file
        File fileToBeParsed = new File("src/com/gryffonx/xml/npcs.xml");
        Document xmlDoc = null;
        SAXReader reader = new SAXReader();

        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //find the length
        int listLength = xmlDoc.selectNodes("npcs/npc").size();

        //loop the list
        for (int i = 0; i < listLength; i++) {
            int id = i;
            String chaId;
            String name;
            String race;
            String hitpoints;
            String characterclass;


            chaId = Integer.toString(i);
            name = xmlDoc.selectSingleNode("npcs/npc[@npc_id='" + id + "']/name").getStringValue();
            race = xmlDoc.selectSingleNode("/npcs/npc[@npc_id='" + id + "']/name/@race").getStringValue();
            hitpoints = xmlDoc.selectSingleNode("npcs/npc[@npc_id='" + id + "']/hitpoints").getStringValue();
            characterclass = xmlDoc.selectSingleNode("npcs/npc[@npc_id='" + id + "']/classes/class").getStringValue();

            Object[] myArry = {chaId, name, race, characterclass, hitpoints};
            generatedTableModel.addRow(myArry);


        }

        //create an array from the values I want

        //use the table model to add a row

        //Add action listeners
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                pnlMainPnl.setVisible(false);
            }
        });

        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                NPCEdit window = new NPCEdit("NPC Editor");
                window.pack();
                window.setVisible(true);
            }
        });

        tblNpcs.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int row = tblNpcs.getSelectedRow();

                    if (row == -1) {
                    } else {
                        System.out.println(tblNpcs.getValueAt(row, 0));
                    }
                }

            }
        });
        btnEdit.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int row = tblNpcs.getSelectedRow();
                //System.out.println(row);
                NPCEdit window = new NPCEdit("NPC Editor", Integer.toString(row));
                window.pack();
                window.setVisible(true);
            }
        });
        btnClose.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                pnlMainPnl.getTopLevelAncestor().setVisible(false);
            }
        });
    }

}
